package cn.yudao.pet.mqconsumer;

import cn.yudao.order.domain.OrderPetAcquisition;
import cn.yudao.order.service.IOrderPetAcquisitionService;
import cn.yudao.pet.service.ISearchMasterMsgService;
import cn.yudao.pet.service.impl.SearchMasterMsgServiceImpl;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "order",selectorExpression="*",consumerGroup = "order_test")
public class RocketMQMsgListener implements RocketMQListener<MessageExt> {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;


    @Override
    public void onMessage(MessageExt message) {
        // 接收消息
        byte[] body = message.getBody();
        // 将消息转换为字符串
        String msg = new String(body, CharsetUtil.UTF_8);
        // string转换为long
        Long id = Long.valueOf(msg);
        // 根据id得到订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionService.LoadById(id);
        // 判断订单状态
        if (orderPetAcquisition.getState() == 0){
            // 调用方法处理延迟订单
            searchMasterMsgService.cancel(id);
        }
        System.out.println(msg);
        log.info("接收到消息：{}", msg);
    }

}